package cn.ljl.org.service.impl;

import cn.ljl.basic.service.impl.BaseServiceImpl;
import cn.ljl.basic.util.Md5Utils;
import cn.ljl.basic.util.StrUtils;
import cn.ljl.org.domain.Employee;
import cn.ljl.org.mapper.EmployeeMapper;
import cn.ljl.org.service.IEmployeeService;
import cn.ljl.user.domain.Logininfo;
import cn.ljl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Transactional
    @Override
    public void del(Long id) {
        //删除t_logininfo信息
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.del(employee.getLogininfo_id());

        //删除当前员工的角色信息:t_employee_role
        employeeMapper.delRoleByEmployeeId(id);

        //删除t_employee信息
        super.del(id);
    }

    @Transactional
    @Override
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //创建Logininfo对象
        Logininfo logininfo = employee2Logininfo(employee);
        //先保存Logininfo对象 - 生成自增id
        logininfoMapper.save(logininfo);
        //将id设置到employee对象中
        employee.setLogininfo_id(logininfo.getId());
        //添加管理员信息
        super.save(employee);

        //添加当前员工的角色信息
        if(employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }

    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //通过属性同名原则进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;
    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //注意:修改的时候不应该修改密码 - 强制修改密码

        //创建Logininfo对象:拷贝了6个数据包括id[employee的id]
        Logininfo logininfo = employee2Logininfo(employee);
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        //重新设置Logininfoid
        logininfo.setId(dbEmployee.getLogininfo_id());
        Logininfo dbLogininfo = logininfoMapper.loadById(dbEmployee.getLogininfo_id());
        logininfo.setDisable(dbLogininfo.getDisable());

        //操作数据库
        logininfoMapper.update(logininfo);
        //employee - 前端传递的
        employee.setShop_id(dbEmployee.getShop_id());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        super.update(employee);

        //修改 = 先删再添加
        employeeMapper.delRoleByEmployeeId(employee.getId());
        if(employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }
}
